package tianwang.stat;

import java.util.Arrays;
import java.util.List;

import tianwang.dao.WordLevelDao;
import tianwang.model.AticleModel;
import tianwang.model.WordsDaoEntry;
import tianwang.services.collect.CollectResult;
import tianwang.services.collect.Collecter;
/**
 * 
 * 计算单词词频的中位数
 * 
 */
public class WordsMedianFreqCollecter implements Collecter<CollectResult> {

	private WordLevelDao wordLevelDao = null;
	
	public WordsMedianFreqCollecter(){
		this.wordLevelDao = new WordLevelDao();
	}
	
	@Override
	public CollectResult collect(Object material) throws Exception {
		AticleModel model = (AticleModel) material;
		List<String> list = model.getEntityWords();
		if(list == null){
			return null;
		}
		
		
		int listLen = list.size();
		int[] articleWordList = new int[listLen];
		
		int count = 0;
		WordsDaoEntry word = null;
		for(String entry : list){
			 word = this.wordLevelDao.getWord(entry);
			 if(word == null){
				 System.err.println(model.getName() + " :: " + entry);
				 continue;
			 }
			 
			 articleWordList[count] = word.getFreq();
			 
			 count ++;
		}
		
		Arrays.sort(articleWordList);
		
		final int  result = articleWordList[listLen/2];
		
		return new CollectResult(){

			@Override
			public Object pump() {
				return result;
			}
			
		};
	}
	
	
	public static void main(String[] a) throws Exception{
		WordsMedianFreqCollecter collecter = new WordsMedianFreqCollecter();
		
		AticleModel model = new AticleModel();
		model.setAticle("I love fuck you .");
		
		System.out.println(collecter.collect(model).pump());
	}
}
